import { createRouter, createWebHistory } from 'vue-router'
import {getToken} from '@/utils/cookie'
const asyncRouter=[
  {
    path:'/',
    redirect:'/home',
  },
  {
    path:'/home',
    name:'home',
    component:()=>import('@/views/Home/Home.vue'),
    meta:{
      isShowNav:true,
    }
  },
  {
    path:'/recommend',
    name:'recommend',
    component:()=>import('@/views/recommend/recommend.vue'),
    meta:{
      isShowNav:true,
    
      needLogin:true
    }
  },
  {
    path:'/cart',
    name:'cart',
    component:()=>import('@/views/Cart/Cart.vue'),
    meta:{
      isShowNav:true,
      title:"购物车"
    }
  },
  {
    path:'/user',
    name:'user',
    component:()=>import('@/views/User/User.vue'),
    meta:{
      isShowNav:true,
      title:"我的"
    }
  },
  {
    path:'/login',
    name:'login',
    component:()=>import('@/views/Login/Login.vue'),
    meta:{
      isShowNav:false,
      title:'登录'
    }
  },
  {
    path:'/goods/:id',
    name:'goods',
    component:()=>import('@/views/Home/GoodsDetail.vue'),
    meta:{
      title:'商品详情'
    }

  },
  {
    path: '/:toHome*',
    name: 'toHome',
    redirect: '/home',
  },
  {
    path:'/address-list',
    name:'addressList',
    component:()=>import('@/views/Address/Address.vue'),
    meta:{
        needLogin:true,
        title:'收货地址'

    }
  },
  {
    path:'/edit-address',
    name:'editAddress',
    component:()=>import('@/views/Address/EditAddress.vue'),
    meta:{
      needLogin:true,
     
    }
  },
  {
    path:'/create-order',
    name:'createOrder',
    component:()=>import('@/views/Order/Created.vue'),
    meta:{
      needLogin:true,
      title:'生成订单'

  }
  },
  {
    path:'/order',
    name: 'order',
  component: () => import('@/views/Order/list.vue'),
  meta: {
    title: '我的订单',
    needLogin:true,
  },
  },
  {
    path: '/add-article',
    name: 'addArticle',
    component: () => import('@/views/Recommend/Add.vue'),
    meta: {
      title: '新增种草文章',
      needLogin:true,
    },
  },
  {
    path: '/article-detail',
    name: 'articleDetail',
    component: () => import('@/views/Recommend/Detail.vue'),
    meta: {
      title: '种草文章详情',
      needLogin:true,
    },
  },
  {
    path:'/my-artical',
    name:'myArticle',
    component:()=>import('@/views/recommend/recommend.vue'),
    mata:{
      needLogin:true,
    }
  },
  {
    path:'/thumbs-up',
    name:'ThumbsUp',
    component:()=>import('@/views/User/ThumbsUp.vue'),
    meta: {
      title: '我的点赞',
      needLogin:true,
    },
  },
  {
    path: '/my-collection',
    name: 'myCollection',
    component: () => import('@/views/User/Collection.vue'),
    meta: {
      title: '我的收藏',
      needLogin:true,
    },
  },
  {
    path:'/change-password',
    name:'changePassword',
    component:()=>import('@/views/Login/changePassword.vue'),
    meta:{
      title:'修改密码',
      needLogin:true
    }
  }
    
  

  
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
  ...asyncRouter
  ]
})


router.beforeEach((to,from,next)=>{
//先判断是否登录
if(getToken()){
    if(to.path==='/login'){
      next(from.path)
    }else{
      next()
  }
}else{
    if(to.meta.needLogin){
      next('/login')
    }else{
      next()
    } 
}


})

export default router
